package main

import (
	. "SGEngine"
	. "SGEngine/UI"
	. "chipmunk"
	//"fmt"
	"github.com/glfw"
	"math"
	"math/rand"
	"time"
)

var W *Window
var q *Quad
var c3d *Camera2D

func main() {
	W = CreateWindow(1000, 700, "-_-_-OoOoOoOoOOOoOO_-_-_-")
	W.MainScene.BgColor = NewColor3Byte(255, 255, 255)
	c3d = NewCamera2D().(*Camera2D)
	W.MainScene.AddCamera(c3d)
	glfw.SetKeyCallback(KeyHander)
	InitScene()
	InitUI()
	W.Run()

}

var (
	space *Space
)

const (
	NotGrabable = Layers(1)
)

func InitUI() {
	btn := NewButton()
	btn.ClickFunc = func() {
		var body Body
		w := 15.0 + 15.0*rand.Float64()
		h := 15.0 + 15.0*rand.Float64()
		body = space.AddBody(BodyNew(1.0, MomentForBox(1.0, w, w)))
		space.AddShape(BoxShapeNew(body, w, h))
		//q := NewQuad(float64(w), float64(h))
		q := NewACImage("a.jpg")
		q.GetTransfrom().SetSize(float64(w), float64(w), 0)
		AddChild(W.MainScene, q)
		body.SetUserData(q)
	}
	AddChild(W.MainScene, btn)
	btn.SetPosition(100, 0, 0)
}

func InitScene() {
	rand.Seed(time.Now().UnixNano())
	s := SpaceNew()
	space = &s
	ACTimeUpdate.AddUpdate(Update)
	space.SetIterations(30)
	space.SetGravity(VectNew(0.0, -100.0))
	space.SetSleepTimeThreshold(0.5)
	space.SetCollisionSlop(0.5)

	static := space.StaticBody()

	shape := space.AddShape(SegmentShapeNew(static, VectNew(-640.0, -240.0), VectNew(640.0, -240.0), 0.0))
	shape.SetElasticity(1.0)
	shape.SetFriction(1.0)
	shape.SetLayers(NotGrabable)

	for i := 0; i < 4; i++ {
		for j := 0; j < 4; j++ {
			var body Body
			w := 15.0 + 15.0*rand.Float64()
			h := 15.0 + 15.0*rand.Float64()
			body = space.AddBody(BodyNew(1.0, MomentForBox(1.0, w, h)))
			shape = space.AddShape(BoxShapeNew(body, w, h))
			q := NewQuad(float64(w), float64(h))
			AddChild(W.MainScene, q)
			body.SetUserData(q)
			q.MColor = NewColor4(rand.Float32(), rand.Float32(), rand.Float32(), 1)
			r1 := 8.0 * (0.5 - rand.Float64())
			r2 := 8.0 * (0.5 - rand.Float64())
			x := r1 + float64(j)*32.0 - 250.0
			y := r2 + 500.0 - float64(i)*32.0
			body.SetPosition(VectNew(x, y))
			shape.SetElasticity(0.5)
			shape.SetFriction(0.8)

		}
	}

}

func Update(dt float64, this interface{}) {

	space.Step(1.0 / 20.0 / 3.0)
	space.Step(1.0 / 20.0 / 3.0)
	space.Step(1.0 / 20.0 / 3.0)

	space.EachBody(func(b Body) {
		UD := b.UserData()

		if UD != nil {
			if _, ok := UD.(*Quad); ok == true {
				UD.(*Quad).SetPosition(float64(b.Position().X), float64(b.Position().Y), 0)
				UD.(*Quad).Rotate(float64(b.Angle())*180.0/math.Pi, 0, 0, 1)
			} else {
				UD.(*ACImage).GetTransfrom().SetPosition(float64(b.Position().X), float64(b.Position().Y), 0)
				UD.(*ACImage).GetTransfrom().Rotate(float64(b.Angle())*180.0/math.Pi, 0, 0, 1)
			}
		}
	})
}

func KeyHander(key, state int) {

	//W UP
	if key == 87 && state == 0 {
		var body Body
		w := 15.0 + 15.0*rand.Float64()
		h := 15.0 + 15.0*rand.Float64()
		body = space.AddBody(BodyNew(1.0, MomentForBox(1.0, w, h)))
		space.AddShape(BoxShapeNew(body, w, h))
		q := NewQuad(float64(w), float64(h))
		AddChild(W.MainScene, q)
		q.MColor = NewColor4(rand.Float32(), rand.Float32(), rand.Float32(), 1)
		body.SetUserData(q)

	}
}
